package com.tij.rtti.typeinfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:14-2-2
 * @author:
 * @Description: TODO
 * data:2024/10/6 20:37
 * @since
 **/
class CountedInteger{
    public CountedInteger() {

        System.out.println("a ----- " + counter);
    }

    public CountedInteger(int a) {
        System.out.println("----- " + a);
    }

    private static long counter;
    private final long id = counter ++;
    public String toString(){
        return Long.toString(id);
    }
}

public class FilledList<T> {
    private Class<T> type;
    public FilledList(Class<T> type) {
        this.type = type;
    }

    public List<T> create(int nElements){
        List<T> result = new ArrayList<T>();
        try {
        for(int i=0 ; i< nElements;i++){
            result.add(type.newInstance());
        }
        } catch ( Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public static void main(String[] args) {
        FilledList<CountedInteger> f1 = new FilledList<>(CountedInteger.class);
        System.out.println(f1.create(20));
    }
}
